﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmPhieuYeuCau : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private MatHangBOCollection MatHangBOCollection = null;
        private NhomHangBOCollection nhomHangBOCollection = null;
        private PhieuYeuCauBOCollection PhieuYeuCauBOCollection = null;
        private PhieuYeuCauChiTietBOCollection PhieuYeuCauChiTietBOCollection = PhieuYeuCauChiTietBOCollection.NewPhieuYeuCauChiTietBOCollection();
        private PhieuYeuCauBO PhieuYeuCauBO = PhieuYeuCauBO.NewRootPhieuYeuCauBO();

        private string COLUMN_FIRST_MatHang = "Ten";
        private string COLUMN_SECOND_MatHang = "SoThuTu";

        private string COLUMN_FIRST_PhieuYeuCauChiTiet = "Ten";
        private string COLUMN_SECOND_PhieuYeuCauChiTiet = "HeSo";
        #endregion variant

        //=====================================
        #region form
        public FrmPhieuYeuCau()
        {
            InitializeComponent();

            this.Load += new EventHandler(FrmPhieuYeuCau_Load);
            this.FormClosing += new FormClosingEventHandler(FrmPhieuYeuCau_FormClosing);
            this.KeyDown += new KeyEventHandler(this.FrmHoadonnhap_KeyDown);

            this.btnAdd.Click += new EventHandler(btnAdd_Click);
            this.btnXoa.Click += new EventHandler(btnDelete_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.btnRefreshMatHang.Click += new EventHandler(btnRefreshMatHang_Click);

            this.grdViewMatHang.DoubleClick += new System.EventHandler(this.grdViewMatHang_DoubleClick);
            this.gridViewPhieuYeuCau.RowCellClick += new RowCellClickEventHandler(this.gridViewPhieuYeuCau_RowCellClick);

            this.grdViewMatHang.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);
            this.grdViewPhieuYeuCauChiTiet.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewMatHang.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
            this.grdViewPhieuYeuCauChiTiet.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmPhieuYeuCau_Load(object sender, EventArgs e)
        {
            RefreshData();
            //SetFormStatus(this.formStatus);

            Utility.InitControl(this.reItemLookUpEditDonViTinh);
            Utility.InitControl(this.reItemLookUpEditMatHang);
            Utility.InitControl(this.reItemLookUpEditKhachHangID);

            Utility.InitControl(this.lookUpNhanVienID);
            Utility.InitControl(this.lookUpEditKhachHang);

            this.grdViewMatHang.FocusedRowChanged += new FocusedRowChangedEventHandler(grdViewMatHang_FocusedRowChanged);
            this.grdView.FocusedRowChanged += new FocusedRowChangedEventHandler(grdView_FocusedRowChanged);
            this.txtTimkiem.EditValueChanged += new System.EventHandler(this.txtTimkiem_EditValueChanged);
        }

        private void FrmPhieuYeuCau_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }

        private void FrmHoadonnhap_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyData == Keys.Delete)
            {
                int _idMatHang = StaticUtil.GetInt(this.grdViewPhieuYeuCauChiTiet.GetFocusedRowCellValue("MatHangID"),-1);
                int _idDonViTinh = StaticUtil.GetInt(this.grdViewPhieuYeuCauChiTiet.GetFocusedRowCellValue("DonViTinhID"), -1);
                if ((_idMatHang >= 0) & (_idDonViTinh >= 0))
                    {
                        DeleteMatHang(_idMatHang, _idDonViTinh);
                    }
                    else
                    {
                        MessageBox.Show("Bạn chưa chọn Vật tư để xóa! Vui lòng chọn lại", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.grdViewPhieuYeuCauChiTiet.Focus();
                    }
             }
            
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnRefreshMatHang_Click(object sender, EventArgs e)
        {
            FilterMatHangByName("");
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNew();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int _phieuYeuCauID = StaticUtil.GetInt(this.PhieuYeuCauBO.PhieuYeuCauID, -1);
            if (_phieuYeuCauID != -1)
            {
                if (MessageBox.Show("Bạn muốn xóa Phiếu yêu cầu này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Model.PhieuYeuCauBO.DeletePhieuYeuCauBO(_phieuYeuCauID);
                    for (int k = 0; k < PhieuYeuCauChiTietBOCollection.Count; k++)
                    {
                        PhieuYeuCauChiTietBO.DeletePhieuYeuCauChiTietBO((int)PhieuYeuCauChiTietBOCollection[k].PhieuYeuCauChiTietID);
                    }
                    RefreshData();
                    btnAdd_Click(e, e);
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn Phiếu yêu cầu để xóa! ", "Thông báo", MessageBoxButtons.OK);
            }
        }
        #endregion button

        //=====================================
        #region grid
        private void grdViewMatHang_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            //FilterMatHang();
        }

        private void grdView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            FilterMatHang();
        }


        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewMatHang.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }

        private void grdViewMatHang_DoubleClick(object sender, EventArgs e)
        {
            UpdatePhieuYeuCau();
        }



        private void gridViewPhieuYeuCau_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            int phieuYeuCauID = StaticUtil.GetInt(gridViewPhieuYeuCau.GetFocusedRowCellValue("PhieuYeuCauID"));
            //int kiemtraID = kiemtra(HoadonnhapChitietBOCollection, (int)_id);
            //if (kiemtraID == -1)
            //{
            this.PhieuYeuCauBO = Model.PhieuYeuCauBO.GetRootPhieuYeuCauBO(phieuYeuCauID);
            this.PhieuYeuCauChiTietBOCollection = Model.PhieuYeuCauChiTietBOCollection.GetPhieuYeuCauChiTietBOCollection("PhieuYeuCauID = " + phieuYeuCauID.ToString());

            lookUpEditKhachHang.EditValue = PhieuYeuCauBO.KhachHangID.Value;
            lookUpNhanVienID.EditValue = PhieuYeuCauBO.NguoiThucHienID.Value;
            txtMaSo.Text = PhieuYeuCauBO.MaSo;
            txtNoidung.Text = PhieuYeuCauBO.NoiDung;
            dtNgayLapPhieu.DateTime = PhieuYeuCauBO.NgayLapPhieu.Value;

            //bdSourcePhieuYeuCauChiTiet.DataSource = PhieuYeuCauChiTietBOCollection.Clone();


            sapxep();
        }


        #endregion grid


        //=====================================
        #region Texbox

        private void txtTimkiem_EditValueChanged(object sender, EventArgs e)
        {
            FilterMatHangByName(txtTimkiem.Text.Trim());
        }

        #endregion Texbox

        //=====================================
        #region function
        private void FilterMatHang()
        {
            Session.NhomHangID = StaticUtil.GetRealInt(this.grdView.GetFocusedRowCellValue("NhomHangID"));

            if (Session.NhomHangID == -1)
            {
                //this.grdViewPhieuYeuCauChiTiet.ActiveFilterString = "MatHangID=" + int.MaxValue;

                this.MatHangBOCollection = MatHangBOCollection.GetMatHangBOCollection();
                this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            }
            else
            {
                //this.grdViewPhieuYeuCauChiTiet.ActiveFilterString = "MatHangID=" + int.MaxValue;

                this.MatHangBOCollection = MatHangBOCollection.GetMatHangBOCollection("NhomHangID = "+ Session.NhomHangID.ToString());
                this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            }
            //this.grdViewPhieuYeuCauChiTiet.CheckLoaded();
            //SetSttPhieuYeuCauChiTiet();
        }

        private void FilterMatHangByName(string nameMatHang)
        {
            this.MatHangBOCollection = MatHangBOCollection.GetMatHangBOCollection("Ten like N'%" + nameMatHang + "%'");
            this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
        }

        private void ResetData()
        {
            NhanVienBOList nhanVienBOList = NhanVienBOList.GetNhanVienBOList();
            Utility.SetData(this.lookUpNhanVienID, nhanVienBOList, nhanVienBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.lookUpEditKhachHang, khachHangBOList, khachHangBOList.Count);

            txtMaSo.Text = "";
            txtNoidung.Text = "";
            dtNgayLapPhieu.DateTime = DateTime.Now;
            this.PhieuYeuCauBO = Model.PhieuYeuCauBO.NewRootPhieuYeuCauBO();
            this.PhieuYeuCauChiTietBOCollection = Model.PhieuYeuCauChiTietBOCollection.NewPhieuYeuCauChiTietBOCollection();
            bdSourcePhieuYeuCauChiTiet.DataSource = this.PhieuYeuCauChiTietBOCollection.Clone();
        }

        private void RefreshData()
        {
            ResetData();
            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reItemLookUpEditDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reItemLookUpEditMatHang, matHangBOList, matHangBOList.Count);

            KhachHangBOList khachHangBOList = KhachHangBOList.GetKhachHangBOList();
            Utility.SetData(this.reItemLookUpEditKhachHangID, khachHangBOList, khachHangBOList.Count);
            //this.bdSourceKhachHang.DataSource = khachHangBOList.Clone();
            //this.lookUpKhachHangID.Properties.DataSource = khachHangBOList;

            #endregion BOList

            this.MatHangBOCollection = MatHangBOCollection.GetMatHangBOCollection();
            //this.PhieuYeuCauChiTietBOCollection = PhieuYeuCauChiTietBOCollection.GetPhieuYeuCauChiTietBOCollection();
            this.PhieuYeuCauBOCollection = Model.PhieuYeuCauBOCollection.GetPhieuYeuCauBOCollection();
            this.nhomHangBOCollection = NhomHangBOCollection.GetNhomHangBOCollection();

            NhomHangBO NewNhomHangBO = NhomHangBO.NewRootNhomHangBO();
            NewNhomHangBO.Ten = "Tất cả";
            NewNhomHangBO.NhomHangID = -1;
            this.nhomHangBOCollection.Add(NewNhomHangBO);

            this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            //this.bdSourcePhieuYeuCauChiTiet.DataSource = this.PhieuYeuCauChiTietBOCollection.Clone();
            this.bdSourceNhomHang.DataSource = this.nhomHangBOCollection.Clone();
            this.bdSourcePhieuYeuCau.DataSource = this.PhieuYeuCauBOCollection.Clone();


            FilterMatHang();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            //SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void AddNew()
        {
            this.PhieuYeuCauBO = Model.PhieuYeuCauBO.NewRootPhieuYeuCauBO();
            this.PhieuYeuCauChiTietBOCollection = Model.PhieuYeuCauChiTietBOCollection.NewPhieuYeuCauChiTietBOCollection();
            ResetData();
        }

        private void Save()
        {
            if (kiemtraLuu() == 1)
            {
                this.PhieuYeuCauBO.KhachHangID = Convert.ToInt32(lookUpEditKhachHang.EditValue);
                this.PhieuYeuCauBO.MaSo = txtMaSo.Text.Trim();
                this.PhieuYeuCauBO.NgayLapPhieu = dtNgayLapPhieu.DateTime;
                this.PhieuYeuCauBO.NoiDung = txtNoidung.Text.Trim();
                this.PhieuYeuCauBO.NguoiThucHienID = Convert.ToInt32(lookUpNhanVienID.EditValue);
                this.PhieuYeuCauBO.Save();

                int recordCount = this.PhieuYeuCauChiTietBOCollection.Count;
                if (recordCount > 0)
                {
                    for (int i = 0; i < recordCount; i++)
                    {
                        this.PhieuYeuCauChiTietBOCollection[i].PhieuYeuCauID = this.PhieuYeuCauBO.PhieuYeuCauID.Value;
                    }

                    this.PhieuYeuCauChiTietBOCollection.Save();
                }

                MessageBox.Show("Cập nhật thành công! ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.PhieuYeuCauChiTietBOCollection = this.PhieuYeuCauChiTietBOCollection.Save();
                this.PhieuYeuCauBOCollection = Model.PhieuYeuCauBOCollection.GetPhieuYeuCauBOCollection();
                this.bdSourcePhieuYeuCau.DataSource = this.PhieuYeuCauBOCollection.Clone();
                this.bdSourcePhieuYeuCauChiTiet.DataSource = this.PhieuYeuCauChiTietBOCollection.Clone();
            }


            FilterMatHang();
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            //SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            this.bdSourceMatHang.DataSource = this.MatHangBOCollection.Clone();
            this.bdSourcePhieuYeuCauChiTiet.DataSource = this.PhieuYeuCauChiTietBOCollection.Clone();

            FilterMatHang();
        }

        private void UpdatePhieuYeuCau()
        {
            int MatHangID = StaticUtil.GetInt(grdViewMatHang.GetFocusedRowCellValue("MatHangID"));
            //int kiemtraID = kiemtra(HoadonnhapChitietBOCollection, (int)_id);
            //if (kiemtraID == -1)
            //{
            PhieuYeuCauChiTietBO newBO = PhieuYeuCauChiTietBO.NewRootPhieuYeuCauChiTietBO();
            newBO.MatHangID = MatHangID;
            PhieuYeuCauChiTietBOCollection.Add(newBO);

            sapxep();
            
        }
        
        private void sapxep()
        {

            int length = PhieuYeuCauChiTietBOCollection.Count;

            for (int i = 0; i < length; i++)
            {
                PhieuYeuCauChiTietBOCollection[i].STT0 = (i + 1);
            }

            bdSourcePhieuYeuCauChiTiet.DataSource = PhieuYeuCauChiTietBOCollection;
        }

        private void DeleteMatHang(int mathangID,int donvitinhID)
        {
            
            if (MessageBox.Show("Bạn có muốn xóa Mặt Hàng và Đơn vị tính này không? ", "Cảnh báo!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int _idphieuyeucau = StaticUtil.GetInt(this.grdViewPhieuYeuCauChiTiet.GetFocusedRowCellValue("PhieuYeuCauChiTietID"),-1);

                if (_idphieuyeucau >= 0)
                {
                    PhieuYeuCauChiTietBO.DeletePhieuYeuCauChiTietBO(_idphieuyeucau);
                }
                PhieuYeuCauChiTietBOCollection.Remove(mathangID,donvitinhID);
                sapxep();
            }
        }

        private int kiemtraLuu()
        {
            int tempkiemtra = 1;
            string strError = "";

            if (lookUpEditKhachHang.EditValue == null)
            {
                tempkiemtra = 0;
                strError = "Chưa chọn Khách hàng";
            }

            if (lookUpNhanVienID.EditValue == null)
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa chọn Người thực hiện";
            }

            if (txtMaSo.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Mã số";
            }

            if (txtNoidung.Text.Trim() == "")
            {
                tempkiemtra = 0;
                strError = strError + " ;Chưa nhập Nội dung";
            }
            if (tempkiemtra == 0)
                MessageBox.Show(strError, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return tempkiemtra;
        }

        #endregion function
    }
}